/* 
 * Copyright 2008 Lovely Systems GmbH
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lovely.gwt.jsonrpc.client;

import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.json.client.JSONBoolean;
import com.google.gwt.json.client.JSONNull;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;

/**
 * Base class for generated JSON service wrappers, contains some utility methods
 * + some settings (address, user, password etc).
 */
public class JSONServiceBase implements JSONServiceDefTarget {
	protected String serviceEntryPoint;
	protected String user;
	protected String password;
	protected boolean appendMethodNameToAddress;

	public JSONServiceBase() {
		appendMethodNameToAddress = true;
	}

	protected Boolean deserialize_java_lang_Boolean(JSONValue value) {
		if (value.isNull() != null)	return null;
		return new Boolean(((JSONBoolean) value).booleanValue());
	}

	protected Byte deserialize_java_lang_Byte(JSONValue value) {
		if (value.isNull() != null)	return null;
		return new Byte((byte) ((JSONNumber) value).doubleValue());
	}

	protected Short deserialize_java_lang_Short(JSONValue value) {
		if (value.isNull() != null)	return null;
		return new Short((short) ((JSONNumber) value).doubleValue());
	}

	protected Integer deserialize_java_lang_Integer(JSONValue value) {
		if (value.isNull() != null)	return null;
		return new Integer((int) ((JSONNumber) value).doubleValue());
	}

	protected Long deserialize_java_lang_Long(JSONValue value) {
		if (value.isNull() != null)	return null;
		return new Long((long) ((JSONNumber) value).doubleValue());
	}

	protected Float deserialize_java_lang_Float(JSONValue value) {
		if (value.isNull() != null)	return null;
		return new Float((float) ((JSONNumber) value).doubleValue());
	}

	protected Double deserialize_java_lang_Double(JSONValue value) {
		if (value.isNull() != null)	return null;
		return new Double(((JSONNumber) value).doubleValue());
	}

	protected char deserialize_java_lang_Character(JSONValue value) {
		// TODO: check case if value == null
		return ((JSONString) value).stringValue().charAt(0);
	}

	protected String deserialize_java_lang_String(JSONValue value) {
		if (value.isNull() != null)	return null;
		return ((JSONString) value).stringValue();
	}

	protected boolean deserialize_boolean(JSONValue value) {
		return ((JSONBoolean) value).booleanValue();
	}

	protected byte deserialize_byte(JSONValue value) {
		return (byte) ((JSONNumber) value).doubleValue();
	}

	protected short deserialize_short(JSONValue value) {
		return (short) ((JSONNumber) value).doubleValue();
	}

	protected int deserialize_int(JSONValue value) {
		return (int) ((JSONNumber) value).doubleValue();
	}

	protected long deserialize_long(JSONValue value) {
		return (long) ((JSONNumber) value).doubleValue();
	}

	protected float deserialize_float(JSONValue value) {
		return (float) ((JSONNumber) value).doubleValue();
	}

	protected double deserialize_double(JSONValue value) {
		return ((JSONNumber) value).doubleValue();
	}

	protected char deserialize_char(JSONValue value) {
		return ((JSONString) value).stringValue().charAt(0);
	}

	protected JSONBoolean serialize_boolean(boolean value) {
		return JSONBoolean.getInstance(value);
	}

	protected JSONNumber serialize_byte(byte value) {
		return new JSONNumber(value);
	}

	protected JSONNumber serialize_short(short value) {
		return new JSONNumber(value);
	}

	protected JSONNumber serialize_int(int value) {
		return new JSONNumber(value);
	}

	protected JSONNumber serialize_long(long value) {
		return new JSONNumber(value);
	}

	protected JSONNumber serialize_float(float value) {
		return new JSONNumber(value);
	}

	protected JSONNumber serialize_double(double value) {
		return new JSONNumber(value);
	}

	protected JSONString serialize_char(char value) {
		return new JSONString(String.valueOf(value));
	}

	protected JSONValue serialize_java_lang_Boolean(Boolean value) {
		if (value == null)
			return JSONNull.getInstance();
		return JSONBoolean.getInstance(value.booleanValue());
	}

	protected JSONValue serialize_java_lang_Byte(Byte value) {
		if (value == null)
			return JSONNull.getInstance();
		return new JSONNumber(value);
	}

	protected JSONValue serialize_java_lang_Short(Short value) {
		if (value == null)
			return JSONNull.getInstance();
		return new JSONNumber(value);
	}

	protected JSONValue serialize_java_lang_Integer(Integer value) {
		if (value == null)
			return JSONNull.getInstance();
		return new JSONNumber(value);
	}

	protected JSONValue serialize_java_lang_Long(Long value) {
		if (value == null)
			return JSONNull.getInstance();
		return new JSONNumber(value);
	}

	protected JSONValue serialize_java_lang_Float(Float value) {
		if (value == null)
			return JSONNull.getInstance();
		return new JSONNumber(value);
	}

	protected JSONValue serialize_java_lang_Double(Double value) {
		if (value == null)
			return JSONNull.getInstance();
		return new JSONNumber(value);
	}

	protected JSONValue serialize_java_lang_Character(Character value) {
		if (value == null)
			return JSONNull.getInstance();
		return new JSONString(value.toString());
	}

	protected JSONValue serialize_java_lang_String(String value) {
		if (value == null)
			return JSONNull.getInstance();
		return new JSONString(value);
	}

	protected String addSlashIfNeeded(String s) {
		if (!s.endsWith("/")) {
			return s + "/";
		}
		return s;
	}

	protected RequestBuilder createBuilder(String methodName) {
		String address;
		if (appendMethodNameToAddress) {
			address = addSlashIfNeeded(serviceEntryPoint) + methodName;
		} else {
			address = serviceEntryPoint;
		}
		RequestBuilder result = new RequestBuilder(RequestBuilder.POST, address);
		result.setHeader("Content-type", "application/json; charset=utf-8");
		if (user != null)
			result.setUser(user);
		if (password != null)
			result.setPassword(password);
		return result;
	}

	public String getServiceEntryPoint() {
		return serviceEntryPoint;
	}

	public void setServiceEntryPoint(String serviceEntryPoint) {
		this.serviceEntryPoint = serviceEntryPoint;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean getAppendMethodNameToAddress() {
		return appendMethodNameToAddress;
	}

	public void setAppendMethodNameToAddress(boolean appendMethodNameToAddress) {
		this.appendMethodNameToAddress = appendMethodNameToAddress;
	}

}
